﻿<?php

class IntegralModel extends BaseModel
{
      var $table  = 'integral';	

     function allshow($word='',$words='',$roleid='',$fieldtype='')
     {  
	     if(empty($fieldtype)) $fieldtype = 'user_name';
	     empty($order)?$order = 'time desc':'';

		 $conditions =' 1 ';		 
	 
		 foreach($word as $key => $value)
		 {	
		     if($value)
		     {
		         /* $conditions .= " and {$fieldtype} like '%{$value}%' ";*/
				 $conditions .= " and {$fieldtype}='{$value}' ";	  
		     }
		 }	 
	  	 if($roleid)
		 {
		    $conditions .= " and role_id='{$roleid}' ";
		 }	 
		 
	      //分页时，一定是先调用page函数，再调用S_Sql_Exe函数
		 $info['page'] = $this->Page(array('conditions' =>"{$conditions}",'url' => 'admin/?app=integral&act=index&roleid='.$roleid.'&word='.$words."&fieldtype={$fieldtype}"));		
		 $info['data'] = $this->S_Sql_Exe(array('fields' =>'*','conditions' =>"{$conditions}",'order' =>"{$order}",'limit' =>$info['page']['sql_limit']));
    
		 return $info;
        
     }

     function personlshow($userid=0)
     {  
	      
	     empty($order)?$order = 'time desc':'';
		 
		 $conditions = "userid='{$userid}' ";
		 
	      //分页时，一定是先调用page函数，再调用S_Sql_Exe函数
		 $info['page'] = $this->Page(array('conditions' =>"{$conditions}",'url' => '?app=integral&act=index'));		
		 $info['data'] = $this->S_Sql_Exe(array('fields' =>'*','conditions' =>"{$conditions}",'order' =>"{$order}",'limit' =>$info['page']['sql_limit']));
    
		 return $info;
        
     }

    /* 插入数据 */
    function post($data)
    {    
  		return $this->IU_Sql_Exe(array ('data' => $data, 'mode' => 'INSERT'));
       
    }

    /* 删除数据 */
    function del($id ='')
    {      
		return $this->D_Sql_Exe(array ('conditions' =>"id = '{$id}' "));
        
    }

    /* 删除数据 */
    function del_time($start_time=0,$end_time=0)
    {     
	
	    $str = "$start_time<time and time<$end_time";		 
		return $this->$db->query("DELETE FROM ".$this->$table." WHERE "	.$str. " ");
        
    }

    /* 删除数据 */
    function select_time($uid=0)
    { 
	    $time = time();
	    $c_time = $time-24*60*60 ;
	    $str = "$c_time<time and userid='$uid' ";		 
        $info = $this->S_Sql_Exe(array('fields' =>'id','conditions' =>$str,'limit' =>'1','return' =>'one'));
		return $info['id'];
        
    }	
	
    /* 删除数据 */
    function select_del($checkdata=array())
    {     
			$str='';
			foreach($checkdata as $key => $value)
			{
			  $str.="id='".$value."'  or ";			  		  
			}			
			$str = substr($str, 0,-3);
			
			return $this->$db->query("DELETE FROM ".$this->table_id." WHERE "	.$str. " ");
        
    }	
	
	function integraladd($integral,$uid)
	{
		$sql="update ".$this->_prefix."users set integral = integral + $integral where user_id = ".$uid;
		return $query = $this->db->query($sql); 
	}

	function integralpay($integral,$uid)
	{
			
		$sql="update ".$this->_prefix."users set integral = integral - $integral where user_id = ".$uid;
		return $query = $this->db->query($sql);
		
	}
	function integraltoling()
	{			
		$sql="update ".$this->_prefix."users set integral = 0 where 1 ";
		return $query = $this->db->query($sql);
		
	}

}

?>